﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
	public class NissenSSODao : DaoBase
	{
		private long MaxNo = 1;
		
		public IList<NissenSSO> GetNissenSSOUnUsing()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Machine>();

				IList<Machine> machines = crt.List<Machine>();

				List<string> ssoCode = new List<string>();

				foreach (var machine in machines)
				{
					ssoCode.Add(machine.NissenSSO.NissenSSOCode);
				}

				crt = session.CreateCriteria<NissenSSO>();

				if (ssoCode.Count > 0)
				{
					crt.Add(Restrictions.Not(Restrictions.In("NissenSSOCode", ssoCode)));
				}

				
				crt.Add(Restrictions.Gt("ID", MaxNo));

				return crt.List<NissenSSO>();

			}

		}

        public IList<NissenSSO> GetNissenSSO()
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                ICriteria crt = session.CreateCriteria<NissenSSO>();

                return crt.List<NissenSSO>();
            }

        }

        public NissenSSO GetNissenSSOUnUsingForWorker(Worker newWorker, string ssoMin, string ssoMax)
		{

			using (ISession session = this.SessionFactory.OpenSession())
			{
			    try
			    {
                    session.BeginTransaction();//above committed read OK
                    ICriteria crt = session.CreateCriteria<Worker>();

                    crt.SetLockMode(LockMode.Upgrade); //LockMode.Force also OK
                    IList<Worker> workers = crt.List<Worker>();

                    List<string> ssoCode = new List<string>();

                    foreach (var worker in workers)
                    {
                        ssoCode.Add(worker.NissenSSO.NissenSSOCode);
                    }

                    crt = session.CreateCriteria<NissenSSO>();

                    if (ssoCode.Count > 0)
                    {
                        crt.Add(Restrictions.Not(Restrictions.In("NissenSSOCode", ssoCode)));
                    }

                    crt.Add(Restrictions.Ge("NissenSSOCode", ssoMin));
                    crt.Add(Restrictions.Le("NissenSSOCode", ssoMax));

			        IList<NissenSSO> listNissenSSO = crt.List<NissenSSO>();

                    if(listNissenSSO.Count > 0)
                    {
                        newWorker.NissenSSO = listNissenSSO[0];
                        session.Save(newWorker);
                        return newWorker.NissenSSO;
                    }

                    return null;
			    }
			    catch (Exception e)
			    {
                    session.Transaction.Rollback();
			        throw e;

			    }finally
			    {
                    if(session.Transaction.IsActive)
                        session.Transaction.Commit();
			    }
			}

		}


        public void SaveOrUpdate(NissenSSO nissenSso)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                using (ITransaction t1 = session.BeginTransaction())
                {
                    session.SaveOrUpdate(nissenSso);
                    t1.Commit();
                }
                //session.BeginTransaction();
                //session.SaveOrUpdate(nissenSso);
                //Session.Transaction.Commit();
            }
        }

	    public void UpdateNissenSSOPwd(string pwd)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{
					ICriteria crt = session.CreateCriteria<NissenSSO>();

					IList<NissenSSO> nissenSsos = crt.List<NissenSSO>();

					foreach (var nissenSso in nissenSsos)
					{
						nissenSso.NissenSSOPWD = pwd;
						session.SaveOrUpdate(nissenSso);
					}

					t1.Commit();
				}

			}
		}
	}
}
